package ynu.edu.achitectureclass.service.AirSensorService;

import ynu.edu.achitectureclass.entity.Air_water;
import ynu.edu.achitectureclass.entity.Device;
import ynu.edu.achitectureclass.repository.Air_water_Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.achitectureclass.service.BaseSensorService;
import ynu.edu.achitectureclass.service.SensorService;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class Air_water_Service extends BaseSensorService<Air_water> implements SensorService<Air_water> {

    @Autowired
    private Air_water_Repository airWaterRepository;

    @Transactional
    public void storeSoilData(Object data) {
        // 解析返回的数据并存储
        if (data instanceof List) {  // 确保数据是一个列表
            List<Map<String, Object>> dataList = (List<Map<String, Object>>) data;
            System.out.println(dataList);
            for (Map<String, Object> dataMap : dataList) {
                System.out.println(dataMap);

                String sensorSerialNumber = (String) dataMap.get("id");
                String name = (String) dataMap.get("name");

                // 获取 value 并进行类型转换
                Object valueObj = dataMap.get("value");
                Double value = null;

                if (valueObj instanceof Integer) {
                    value = ((Integer) valueObj).doubleValue() / 10;  // 如果是 Integer，则转换为 Double 并除以 10
                } else if (valueObj instanceof Double) {
                    value = (Double) valueObj / 10;  // 如果已经是 Double，直接除以 10
                } else {
                    throw new IllegalArgumentException("Unexpected type for value: " + valueObj.getClass());
                }

                String timestamp = (String) dataMap.get("timestamp");

                Optional<Device> deviceOpt = deviceRepository.findBySerialNumber(sensorSerialNumber);
                if (!deviceOpt.isPresent()) {
                    throw new IllegalArgumentException("Device with serial number " + sensorSerialNumber + " not found");
                }
                Device device = deviceOpt.get();
                System.out.println(device);

                Air_water airWater = new Air_water();
                airWater.setDeviceId(device.getId());
                airWater.setName(name);
                airWater.setValue(value);  // 设置转换后的值
                LocalDateTime createdAt = LocalDateTime.parse(timestamp.replace("Z", ""));
                airWater.setCreatedAt(createdAt);

                airWaterRepository.save(airWater);
            }
        } else {
            throw new IllegalArgumentException("Invalid data format.");
        }
    }

    @Override
    public Air_water getLatestData() {
        return (Air_water) airWaterRepository.findTopByOrderByCreatedAtDesc()
                .orElseThrow(() -> new IllegalArgumentException("No air temperature data found."));
    }

    @Override
    public List<Air_water> getAllData() {
        return airWaterRepository.findAll();
    }

}
